package com.plume.toolbox.plus.ui;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import com.plume.toolbox.plus.MainActivity;
import com.plume.toolbox.plus.R;

public class DisclaimerDialogFragment extends  DialogFragment{
    private View view;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Inflate the layout for the dialog
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_disclaimer_dialog, null);

        // Find and configure the buttons
        Button btnAgree = view.findViewById(R.id.btn_agree);
        Button btnDisagree = view.findViewById(R.id.btn_disagree);

        btnAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Store user's agreement in SharedPreferences
                storeUserAgreement(true);
                dismiss(); // Close the dialog
            }
        });

        btnDisagree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Exit the application
                showToast("您将不能使用登录功能，下次启动时会再次弹出弹窗");     
                storeUserAgreement(false);
                dismiss(); // Close the dialog
            }
        });

        // Create the AlertDialog with the custom view
        return new AlertDialog.Builder(getActivity())
                .setView(view)
                .create();
    }
    // 在 DialogFragment 或 AlertDialog 中
@Override
public void onResume() {
    super.onResume();
    // Register the key listener when the dialog is active.
    getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                // Ignore the back button press or perform custom logic.
                return true; // Consumed the event.
            }
            return false;
        }
    });
}

@Override
public void onPause() {
    super.onPause();
    // Unregister the key listener when the dialog is inactive.
    getDialog().setOnKeyListener(null);
}
    private void storeUserAgreement(boolean agreed) {
        SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("agreed_to_disclaimer", agreed);
        editor.apply();
    }
    public void showToast(final String message) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
